package Visao;

import Modelo.Hospede;
import Modelo.Reserva;
import RMI.ServicosCliente;
import RMI.ServicosIF;
import Util.Utils;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Pablo Veinberg <pveinberg@gmail.com>
 */
public class FormCheckIn extends javax.swing.JDialog {

    private Reserva reserva;    

    public FormCheckIn(Reserva r) {
        initComponents();
        setLocationRelativeTo(null);
        this.reserva = r;
        this.hospede.setText("Check in de " + r.getHospede().getNome());                
    }

    public Reserva getReserva() {
        return reserva;
    }

    public void setReserva(Reserva reserva) {
        this.reserva = reserva;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        hospede = new javax.swing.JLabel();
        confirma = new javax.swing.JButton();
        cancela = new javax.swing.JButton();

        setTitle("Cadastro de Quartos");
        setModal(true);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        hospede.setText("Check In");

        confirma.setText("Confirma");
        confirma.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                confirmaActionPerformed(evt);
            }
        });

        cancela.setText("Cancela");
        cancela.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(confirma)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cancela))
                    .addComponent(hospede))
                .addContainerGap(49, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(hospede)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(confirma)
                    .addComponent(cancela))
                .addGap(29, 29, 29))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
    }//GEN-LAST:event_formWindowOpened

    private void confirmaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_confirmaActionPerformed

        // seta o quarto como ocupado
        this.reserva.getQuarto().setOcupado(true);
        ServicosIF sc = ServicosCliente.getInstance();
        
        try {                   
            
            if (sc.editReserva(this.reserva) == Utils.SUCESSO) {
                Hospede h = reserva.getHospede(); 
                h.setSaldo(reserva.getTotalHospedagem());
                sc.editHospede(h); 
                
                JOptionPane.showMessageDialog(null, "Checkin realizado com sucesso");
            } else {
                JOptionPane.showMessageDialog(null, "Não foi posível realizar o checkin");
            }
            
        } catch (RemoteException ex) {
            Logger.getLogger(FormCheckIn.class.getName()).log(Level.SEVERE, null, ex);
        } 
        
        this.setVisible(false);        
        this.dispose();
    }//GEN-LAST:event_confirmaActionPerformed

    private void cancelaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelaActionPerformed

        this.setVisible(false);
    }//GEN-LAST:event_cancelaActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancela;
    private javax.swing.JButton confirma;
    private javax.swing.JLabel hospede;
    // End of variables declaration//GEN-END:variables
}
